﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlexiCommerce.Configuration;
using System.ComponentModel.DataAnnotations;
using FlexiCommerce.Localization;

namespace FlexiCommerce.Components
{
    public class Product : PersistenceItem
    {
        private string name;
        [StringLength(20), LocalizedField]
        public String Name
        {
            get
            {
                if (Context.Current.Resolve<EngineSection>().EnableGlobalization)
                {
                    ProductDetails detail = (from d in details where d.Culture.CultureInfo.Name == System.Threading.Thread.CurrentThread.CurrentUICulture.Name select d).SingleOrDefault();
                    if (detail != null)
                    {
                        return detail.Name;
                    }
                }
                return name;
            }
            set
            {
                name = value;
            }
        }
        private IList<ProductDetails> details = new List<ProductDetails>();
        public IList<ProductDetails> Details
        {
            get { return details; }
            set { details = value; }
        }
        public void AddDetail(ProductDetails details)
        {
            details.Product = this;
            Details.Add(details);
        }
        [NonSerialized]
        private IList<Category> categories = new List<Category>();
        [UIHint("Categories")]
        public virtual IList<Category> Categories
        {
            get { return categories; }
            set { categories = value; }
        }
        [NonSerialized]
        private IList<Image> images = new List<Image>();
        [UIHint("Images")]
        public virtual IList<Image> Images
        {
            get { return images; }
            set { images = value; }
        }
        public int DefaultImageID
        {
            get {
                if (images.Count > 0)
                {
                    return images[0].ID;
                }
                else
                {
                    return 0;
                }
            }
            set { }
        }


    }
}
